package com.umeox.watch.moto.gallery.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.umeox.watch.moto.gallery.R;

import java.util.List;


public class PhotoSingleAdapter extends RecyclerView.Adapter<PhotoSingleAdapter.ItemViewHolder> {

    private List<String> list;

    private OnItemClickListener mClickListener;


    public PhotoSingleAdapter(List<String> list, OnItemClickListener mClickListener) {
        this.list = list;
        this.mClickListener = mClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo_preview, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindData(list.get(position));
        holder.itemView.setOnClickListener(v -> mClickListener.onClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void removeItem(int position) {
        list.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, list.size() - position);
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final PhotoView mPhotoIv;


        public ItemViewHolder(View itemView) {
            super(itemView);
            mPhotoIv = itemView.findViewById(R.id.iv_photo);
        }

        public void bindData(String path) {
//            Glide.with(itemView.getContext()).asBitmap().load(path)
//                    .into(new SimpleTarget<Bitmap>() {
//                        @Override
//                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                            mPhotoIv.setImageBitmap(resource);
//                        }
//                    });
            Glide.with(itemView.getContext()).load(path).into(mPhotoIv);
        }
    }


    public interface OnItemClickListener {
        void onClick(int position);
    }
}

